﻿using DistDataProcessor.Data;
using DistDataProcessor.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DistDataProcessor
{
    public class LocalDataBaseHelper
    {
        public static void SaveDistDataGroup(List<MeasureDistanceData> dataGroup)
        {
            if (dataGroup.Count == 0) return;
            var prjId = dataGroup[0].PrjId;
            var new_No = LocalDataBaseHelper.GenerateNewNo(prjId);

            dataGroup.ForEach(p => p.No = new_No);

            using (var db = new LocalSurveyDataBase())
            {
                db.MeasureDistanceDatas.AddRange(dataGroup);
                db.SaveChanges();
            }
        }
        private static int GenerateNewNo(int prjId)
        {
            using (var db = new LocalSurveyDataBase())
            {
                try
                {
                    var no = db.MeasureDistanceDatas.Max(p => p.No);
                    return no + 1;
                }
                catch
                {
                    return 1;
                }

            }
        }
        public static List<MeasureDistanceData> GetDistDataByPrjId(int prjId)
        {
            using (var db = new LocalSurveyDataBase())
            {
                var l = (from p in db.MeasureDistanceDatas
                         where p.PrjId == prjId
                         select p).ToList();
                return l;
            }
        }

        public static List<int> GetDistConfigDevicesIds(int prjId)
        {
            using (var db = new LocalSurveyDataBase())
            {
                var s = (from p in db.DistMeasureConfigs
                         where p.PrjId == prjId
                         orderby p.Order
                         select p.DeviceId).ToList();
                return s;
            }
        }
        /// <summary>
        /// 以新换旧,设置配置点
        /// </summary>
        /// <param name="prjId">工程编号</param>
        /// <param name="devicesNoList">仪器编号</param>
        /// <param name="ringnumberList">环号,可以为null</param>
        public static void SaveDistConfigDatas(int prjId, List<int> devicesNoList, List<int> ringnumberList = null)
        {
            using (var db = new LocalSurveyDataBase())
            {
                //remove existed data configs.
                var list = db.DistMeasureConfigs.Where(p => p.PrjId == prjId);
                db.DistMeasureConfigs.RemoveRange(list);
                //add new list into database
                var newList = new List<DistMeasureConfig>();
                for (var i = 0; i < devicesNoList.Count; i++)
                {
                    var p = new DistMeasureConfig()
                    {
                        RingNumber = ringnumberList == null ? 0 : ringnumberList[i],
                        PrjId = prjId,
                        Order = i,
                        DeviceId = devicesNoList[i]
                    };
                    newList.Add(p);
                }
                db.DistMeasureConfigs.AddRange(newList);
                db.SaveChanges();

            }
        }

        /// <summary>
        /// 输出所有的数据到U盘
        /// </summary>
        /// <param name="prjId">工程ID,同remote_id</param>
        /// <returns>出错信息,为""表示输出成功</returns>
        public static string ExportAllDistDataToUSB(int prjId)
        {
            try
            {
                using (var db = new LocalSurveyDataBase())
                {
                    var lists = (from p in db.MeasureDistanceDatas
                                 where p.PrjId == prjId
                                 orderby p.No
                                 group p by p.No into g
                                 select g).ToList();
                    return DistDataExporter.Export(lists);
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
      
        }
    }
}
